import SelectRegion from '@/views/platform/components/SelectRegion'
import AntModal from '@/components/pt/dialog/AntModal'
import { getSection, addSection, updateSection, findMaxSort, checkCodeUnique } from '@/api/platform/section'
export default {
  name: 'CreateForm',
  props: {
    typeOptions: {
      type: Array,
      required: true
    },
    regionId: {
      type: String,
      required: false
    }
  },
  components: {
    AntModal,
    SelectRegion
  },
  data() {
    const validateCode = (rule, value, callback) => {
      checkCodeUnique(this.form.id, this.form.code).then(response => {
        if (response.data) {
          return callback(new Error('版块编码不能重复'))
        }
        callback()
      })
    }
    const validateRegionId = (rule, value, callback) => {
      if (this.form.type === '1') {
        if (!value) {
          return callback(new Error('请选择地区'))
        }
      }
      callback()
    }
    return {
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      disabled: false,
      selectDisable: false,
      total: 0,
      id: undefined,
      formTitle: '添加版块管理',
      // 表单参数
      form: {
        regionId: this.regionId,
        type: '0'
      },
      rules: {
        type: [{ required: true, message: '请选择版块类型', trigger: 'blur' }],
        name: [
          {
            required: true,
            message: '版块名称不能为空',
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '版块编码不能为空' },
          { validator: validateCode, trigger: 'blur' }
        ],
        regionId: [
          {
            validator: validateRegionId,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  filters: {},
  created() {},
  computed: {},
  watch: {
    'form.regionId': {
      handler(newVal,oldVal) {
        if (newVal) {
          findMaxSort(this.form).then(response => {
            this.form.sort = response.data
          })
        }
      }
    }
  },
  mounted() {},
  methods: {
    onClose() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        regionId: this.regionId,

        type: undefined,

        name: undefined,

        code: undefined,

        sort: undefined
      }
    },
    /** 判断是否有地区 */
    judgeRegion() {
      if (this.regionId !== undefined) {
        this.selectDisable = true
        this.form.type = '1'
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.judgeRegion()
      /** 获取最大编号 */
      findMaxSort(this.form).then(response => {
        this.form.sort = response.data
        this.open = true
        this.formTitle = '添加版块管理'
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.judgeRegion()
      this.open = true
      this.spinning = !this.spinning
      const sectionId = row.id
      getSection(sectionId).then(response => {
        this.form = response.data
        this.formTitle = '修改版块管理'
        this.spinning = !this.spinning
      })
    },
    /** 提交按钮 */
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form))
          if (this.form.id !== undefined) {
            updateSection(saveForm).then(response => {
              this.$message.success('更新成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          } else {
            addSection(saveForm).then(response => {
              this.$message.success('新增成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back() {
      const index = '/platform/section/index'
      this.$router.push(index)
    }
  }
}
